﻿using DotnetSpider.Core;
using DotnetSpider.Core.Processor;
using System;
using System.Collections.Generic;

using AngleSharp.Html.Parser;
using SpiderPlatform.Entity;

namespace SpiderPlatform.Processor
{
    public class WeeeProcessor : BasePageProcessor
    {
        private static HtmlParser htmlParser = new HtmlParser();
        protected override void Handle(Page page)
        {          
            
            List<KouhighEntity> list = new List<KouhighEntity>();  
            var dom = htmlParser.ParseDocument(page.Content);
            var HtmlList = dom.QuerySelectorAll("div.product-section li");
            foreach (var item in HtmlList)
            {
                KouhighEntity entity = new KouhighEntity();
                entity.images = item.QuerySelector("div.media-left img").GetAttribute("data-original");
                entity.name = item.QuerySelector("div.media-body div.media-heading").TextContent.Trim();
                entity.price = item.QuerySelector("div.media-body div.product-bottom-left div.price-block div.price").TextContent;
                entity.id = item.GetAttribute("id");
                entity.link = page.Url;
                var soldOut= item.QuerySelector("div.media-left div.product-sold-out");               
                if (soldOut!=null)
                {
                    entity.is_instock = 1;
                }
                list.Add(entity);
            }
            page.AddResultItem("list",list);
        }
    }  
    
}
